/*     */ package com.zimbra.cs.mailclient;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.Log.Level;
/*     */ import com.zimbra.cs.mailclient.auth.AuthenticatorFactory;
/*     */ import com.zimbra.cs.mailclient.imap.ImapConfig;
/*     */ import com.zimbra.cs.mailclient.imap.ImapConnection;
/*     */ import com.zimbra.cs.mailclient.pop3.Pop3Config;
/*     */ import com.zimbra.cs.mailclient.pop3.Pop3Connection;
/*     */ import com.zimbra.cs.mailclient.smtp.SmtpConfig;
/*     */ import com.zimbra.cs.mailclient.smtp.SmtpConnection;
/*     */ import com.zimbra.cs.mailclient.util.SSLUtil;
/*     */ import java.io.Console;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.PrintStream;
/*     */ import java.util.Arrays;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.ListIterator;
/*     */ import java.util.Map;
/*     */ import java.util.NoSuchElementException;
/*     */ import javax.net.ssl.SSLContext;
/*     */ import javax.security.auth.login.LoginException;
/*     */ import org.apache.log4j.BasicConfigurator;
/*     */ import org.apache.log4j.Level;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class MailClient
/*     */ {
/*     */   private final MailConfig config;
/*     */   protected MailConnection connection;
/*  51 */   private final StringBuilder sbuf = new StringBuilder(132);
/*     */   private String password;
/*     */   private boolean eof;
/*     */   
/*     */   protected MailClient(MailConfig config) {
/*  56 */     this.config = config;
/*     */   }
/*     */   
/*     */   public void run(String[] args) throws LoginException, IOException {
/*  60 */     BasicConfigurator.configure();
/*  61 */     Logger.getRootLogger().setLevel(Level.INFO);
/*  62 */     this.config.getLogger().setLevel(Log.Level.trace);
/*  63 */     parseArguments(args, this.config);
/*  64 */     connect();
/*  65 */     authenticate();
/*  66 */     startCommandLoop();
/*     */   }
/*     */   
/*     */   protected abstract void printUsage(PrintStream paramPrintStream);
/*     */   
/*     */   protected void setPassword(String value) {
/*  72 */     this.password = value;
/*     */   }
/*     */   
/*     */   protected void connect() throws IOException {
/*  76 */     this.config.setConnectTimeout(30);
/*  77 */     this.config.setSSLSocketFactory(SSLUtil.getDummySSLContext().getSocketFactory());
/*  78 */     this.connection = newConnection(this.config);
/*  79 */     this.connection.connect();
/*     */   }
/*     */   
/*     */   protected void authenticate() throws LoginException, IOException {
/*  83 */     Console console = System.console();
/*  84 */     if (console != null) {
/*  85 */       if (this.config.getAuthenticationId() == null) {
/*  86 */         this.config.setAuthenticationId(console.readLine("Username: ", new Object[0]));
/*     */       }
/*  88 */       if ((this.password == null) && (isPasswordRequired())) {
/*  89 */         this.password = new String(console.readPassword("Password: ", new Object[0]));
/*     */       }
/*     */     }
/*  92 */     this.connection.authenticate(this.password);
/*  93 */     String qop = this.connection.getNegotiatedQop();
/*  94 */     if (qop != null) System.out.printf("[Negotiated QOP is %s]\n", new Object[] { qop });
/*     */   }
/*     */   
/*     */   private boolean isPasswordRequired() {
/*  98 */     String mech = this.config.getMechanism();
/*  99 */     if (mech == null) {
/* 100 */       return true;
/*     */     }
/* 102 */     AuthenticatorFactory af = this.config.getAuthenticatorFactory();
/* 103 */     return (af != null) && (af.isPasswordRequired(mech));
/*     */   }
/*     */   
/*     */   private static MailConnection newConnection(MailConfig config) {
/* 107 */     if ((config instanceof ImapConfig))
/* 108 */       return new ImapConnection((ImapConfig)config);
/* 109 */     if ((config instanceof Pop3Config))
/* 110 */       return new Pop3Connection((Pop3Config)config);
/* 111 */     if ((config instanceof SmtpConfig)) {
/* 112 */       return new SmtpConnection((SmtpConfig)config);
/*     */     }
/* 114 */     throw new IllegalArgumentException("Unsupported protocol: " + config.getProtocol());
/*     */   }
/*     */   
/*     */   private void startCommandLoop()
/*     */   {
/* 119 */     Thread t = new ReaderThread(null);
/* 120 */     t.setDaemon(true);
/* 121 */     t.start();
/* 122 */     MailInputStream is = this.connection.getInputStream();
/*     */     try {
/*     */       String line;
/* 125 */       while ((line = is.readLine()) != null) {
/* 126 */         System.out.println(line);
/*     */       }
/*     */     } catch (IOException e) {
/* 129 */       if (!this.eof) e.printStackTrace();
/*     */     }
/*     */   }
/*     */   
/*     */   private class ReaderThread extends Thread {
/*     */     private ReaderThread() {}
/*     */     
/*     */     public void run() {
/* 137 */       try { MailOutputStream os = MailClient.this.connection.getOutputStream();
/*     */         String line;
/* 139 */         while ((line = MailClient.this.readLine(System.in)) != null) {
/* 140 */           if (!MailClient.this.processCommand(line)) {
/* 141 */             os.writeLine(line);
/* 142 */             os.flush();
/*     */           }
/*     */         }
/*     */       } catch (IOException e) {
/* 146 */         e.printStackTrace();
/*     */       }
/* 148 */       MailClient.this.eof = true;
/* 149 */       MailClient.this.connection.close();
/*     */     }
/*     */   }
/*     */   
/*     */   private String readLine(InputStream is) throws IOException {
/* 154 */     this.sbuf.setLength(0);
/*     */     int c;
/* 156 */     while (((c = is.read()) != -1) && (c != 10)) {
/* 157 */       if (c != 13) this.sbuf.append((char)c);
/*     */     }
/* 159 */     return c != -1 ? this.sbuf.toString() : null;
/*     */   }
/*     */   
/*     */   protected void parseArguments(String[] args, MailConfig config) {
/* 163 */     ListIterator<String> itr = Arrays.asList(args).listIterator();
/*     */     try {
/* 165 */       while ((itr.hasNext()) && (parseArgument(itr))) {}
/* 166 */       if (!itr.hasNext()) {
/* 167 */         throw new IllegalArgumentException("Missing required hostname");
/*     */       }
/* 169 */       config.setHost((String)itr.next());
/* 170 */       if (itr.hasNext()) {
/* 171 */         throw new IllegalArgumentException("Extra arguments found after hostname");
/*     */       }
/*     */     } catch (IllegalArgumentException e) {
/* 174 */       System.err.printf("ERROR: %s\n", new Object[] { e });
/* 175 */       printUsage(System.err);
/* 176 */       System.exit(1);
/*     */     }
/*     */   }
/*     */   
/*     */   protected boolean parseArgument(ListIterator<String> args) {
/* 181 */     String arg = (String)args.next();
/* 182 */     if (!arg.startsWith("-")) {
/* 183 */       args.previous();
/* 184 */       return false;
/*     */     }
/* 186 */     if (arg.length() != 2) {
/* 187 */       throw new IllegalArgumentException("Illegal option: " + arg);
/*     */     }
/* 189 */     int minQop = -1;
/* 190 */     int maxQop = -1;
/*     */     try {
/* 192 */       switch (arg.charAt(1)) {
/*     */       case 'p': 
/* 194 */         this.config.setPort(Integer.parseInt((String)args.next()));
/* 195 */         break;
/*     */       case 'u': 
/* 197 */         this.config.setAuthenticationId((String)args.next());
/* 198 */         break;
/*     */       case 'z': 
/* 200 */         this.config.setAuthorizationId((String)args.next());
/* 201 */         break;
/*     */       case 'w': 
/* 203 */         this.password = ((String)args.next());
/* 204 */         break;
/*     */       case 'r': 
/* 206 */         this.config.setRealm((String)args.next());
/* 207 */         break;
/*     */       case 'm': 
/* 209 */         this.config.setMechanism(((String)args.next()).toUpperCase());
/* 210 */         break;
/*     */       case 'k': 
/* 212 */         minQop = parseQop(arg, (String)args.next());
/* 213 */         break;
/*     */       case 'l': 
/* 215 */         maxQop = parseQop(arg, (String)args.next());
/* 216 */         break;
/*     */       case 's': 
/* 218 */         this.config.setSecurity(MailConfig.Security.SSL);
/* 219 */         break;
/*     */       case 't': 
/* 221 */         this.config.setSecurity(MailConfig.Security.TLS_IF_AVAILABLE);
/* 222 */         break;
/*     */       case 'd': 
/* 224 */         this.config.getLogger().setLevel(Log.Level.trace);
/* 225 */         break;
/*     */       case 'q': 
/* 227 */         this.config.getLogger().setLevel(Log.Level.error);
/* 228 */         break;
/*     */       case 'h': 
/* 230 */         printUsage(System.out);
/* 231 */         System.exit(0);
/*     */       case 'e': case 'f': case 'g': case 'i': case 'j': case 'n': case 'o': case 'v': case 'x': case 'y': default: 
/* 233 */         throw new IllegalArgumentException("Illegal option: " + arg);
/*     */       }
/*     */     } catch (NoSuchElementException e) {
/* 236 */       throw new IllegalArgumentException("Option requires argument: " + arg);
/*     */     }
/*     */     
/* 239 */     if (this.config.getSecurity() != MailConfig.Security.SSL) {
/* 240 */       Map<String, String> props = new HashMap();
/* 241 */       props.put("javax.security.sasl.qop", getQop(minQop, maxQop));
/* 242 */       this.config.setSaslProperties(props);
/*     */     }
/* 244 */     return true;
/*     */   }
/*     */   
/*     */   private static int parseQop(String arg, String value) {
/* 248 */     if ((value.equalsIgnoreCase("auth")) || (value.equals("0"))) return 0;
/* 249 */     if ((value.equalsIgnoreCase("auth-int")) || (value.equals("1"))) return 1;
/* 250 */     if ((value.equalsIgnoreCase("auth-conf")) || (value.equals("2"))) return 2;
/* 251 */     throw new IllegalArgumentException("Invalid value for option '" + arg + "'");
/*     */   }
/*     */   
/*     */   private static String getQop(int minQop, int maxQop) {
/* 255 */     if (minQop == -1) minQop = 0;
/* 256 */     if (maxQop == -1) maxQop = 2;
/* 257 */     if (minQop > maxQop) maxQop = minQop;
/* 258 */     StringBuilder sb = new StringBuilder();
/* 259 */     for (int i = maxQop; i >= minQop; i--) {
/* 260 */       switch (i) {
/* 261 */       case 0:  sb.append("auth"); break;
/* 262 */       case 1:  sb.append("auth-int"); break;
/* 263 */       case 2:  sb.append("auth-conf"); break;
/* 264 */       default:  throw new AssertionError();
/*     */       }
/* 266 */       if (i > minQop) sb.append(',');
/*     */     }
/* 268 */     return sb.toString();
/*     */   }
/*     */   
/*     */   private boolean processCommand(String line) throws IOException {
/* 272 */     if (!line.startsWith("!")) return false;
/* 273 */     line = line.substring(1);
/* 274 */     String[] cmdLine = line.split(" ");
/* 275 */     boolean success = false;
/*     */     try {
/* 277 */       success = processCommand(cmdLine);
/*     */     }
/*     */     catch (IndexOutOfBoundsException e) {}
/*     */     
/* 281 */     if (!success) {
/* 282 */       System.out.println(">> ERROR: Invalid command: " + line);
/*     */     }
/* 284 */     return true;
/*     */   }
/*     */   
/*     */   protected boolean processCommand(String[] cmdLine) throws IOException {
/* 288 */     String cmd = cmdLine[0];
/* 289 */     if (isMatch(cmd, "SHow"))
/* 290 */       return processShow(cmdLine);
/* 291 */     if (isMatch(cmd, "SET")) {
/* 292 */       return processSet(cmdLine);
/*     */     }
/* 294 */     return false;
/*     */   }
/*     */   
/*     */   protected boolean processShow(String[] cmdLine) throws IOException {
/* 298 */     return false;
/*     */   }
/*     */   
/*     */   protected boolean processSet(String[] cmdLine) throws IOException {
/* 302 */     return false;
/*     */   }
/*     */   
/*     */   protected static boolean isMatch(String word, String fullword) {
/* 306 */     if (word.length() > fullword.length()) return false;
/* 307 */     boolean enough = false;
/* 308 */     for (int i = 0; i < fullword.length(); i++) {
/* 309 */       char c = fullword.charAt(i);
/* 310 */       if (Character.isLowerCase(c)) {
/* 311 */         enough = true;
/*     */       } else {
/* 313 */         c = Character.toLowerCase(c);
/*     */       }
/* 315 */       if (i >= word.length()) return enough;
/* 316 */       if (Character.toLowerCase(word.charAt(i)) != c) {
/* 317 */         return false;
/*     */       }
/*     */     }
/* 320 */     return word.length() == fullword.length();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailclient/MailClient.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */